var pie = {
		init() {
			this.getData();
			this.optionPie = {
				title: {
					text: '',
					left: 'center',
				},
				legend: {
					orient: 'vertical',
					left: 'left',
					data: [],
				},
				series: {
					name: '',
					type: 'pie',
					radius: '55%',
					center: ['50%', '60%'],
					data: [],
					itemStyle: {
						emphasis: {
							shadowBlur: 10,
							shadowColor: 'rgba(0,0,0,.5)'
						}
					}
				}
			};
			this.optionBar = {
				title: {
					text: '',
					left: 'center',
				},
				color: ['#3398DB'],
				tooltip: {
					trigger: 'axis',
					axisPointer: {            // 坐标轴指示器，坐标轴触发有效
						type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
					}
				},
				grid: {
					left: '3%',
					right: '4%',
					bottom: '3%',
					containLabel: true
				},
				xAxis: [
					{
						type: 'category',
						data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
						axisTick: {
							alignWithLabel: true
						}
					}
				],
				yAxis: [
					{
						type: 'value'
					}
				],
				series: [
					{
						name: '人数',
						type: 'bar',
						barWidth: '60%',
						data: [10, 52, 200, 334, 390, 330, 220]
					}
				]
			}
		},
		getData() {
			var This = this;
			$.ajax({
				url: "./php2/findAllStudent.php",
				type: 'get',
				data:{},
				// dataType: 'json',
				success: function (res) {
					if (res.status) {
						var list=res.data;
						if (list.length > 0) {
							// console.log(list)
							// This.gradeChart(list);
							This.majorChart(list);
							This.sexChart(list);
						} else {
							alert('亲，没有数据哦~');
						}
					} else {
						// console.log(res);
						alert(res.msg);
					}
				},
				error:function (res){
					// console.log(res.responseText);
				}
			})
		},
		majorChart(data) {
			var myChart = echarts.init($('.areaChart')[0]);
			var xAxisData = [];
			var seriesData = [];

			//xAxis[0].data = [智能，计科，网工]
			//series[0].data = [12,55,65]


			var newData = {};
			// {网工：444，计科：888}
			data.forEach(function (item) {
				if (!newData[item.major]) {
					newData[item.major] = 1;
					xAxisData.push(item.major);
				} else {
					newData[item.major]++;
				}
			});

			for (var prop in newData) {
				seriesData.push(newData[prop]);

			}
			this.optionBar.title.text = '重邮学生专业数据统计';
			this.optionBar.xAxis[0].data = xAxisData;
			this.optionBar.series[0].data = seriesData;
			myChart.setOption(this.optionBar);
		},
		sexChart(data) {
			//seriesData= [{name:2018, value:10},...]

			//{2018：10，2019：20}
			var myChart = echarts.init($('.sexChart')[0]);
			var legendData = ['男', '女'];

			var newData = {};
			data.forEach(function (item) {
				if (!newData[item.sex]) {
					newData[item.sex] = 1;
				} else {
					newData[item.sex]++;
				}
			});


			var seriesData = [
				{ name: '女', value: newData[0] },
				{ name: '男', value: newData[1] },
			];
			this.optionPie.title.text = '重邮学生性别统计图';
			this.optionPie.legend.data = legendData;
			this.optionPie.series.name = '性别分布';
			this.optionPie.series.data = seriesData;
			this.optionPie.roseType='angle';
			myChart.setOption(this.optionPie);
		}
}

	pie.init();